#include <string>
#include "Panel.h"
#include "ResourcesManager.h"
#include "Tile.h"

Panel::Panel()
{
    texSelectCursor = ResourcesManager::Instance().texturePool.find(ResID::Tex_UISelectCursor)->second;
}

Panel::~Panel()
{
    SDL_DestroyTexture(texTextBackground);
    SDL_DestroyTexture(texTextForeground);
}

void Panel::Show()
{
    visible = true;
}

void Panel::SetIdxTile(const SDL_Point &idx)
{
    idxTileSelected = idx;
}

void Panel::SetCenterPos(const SDL_Point &pos)
{
    centerPos = pos;
}

void Panel::OnInput(const SDL_Event &event)
{
    if (!visible) return;

    switch (event.type) {
        case SDL_MOUSEMOTION: {
            SDL_Point posCursor = {event.motion.x, event.motion.y};
            SDL_Rect rectTarget = {0, 0, sizeButton, sizeButton};

            rectTarget.x = centerPos.x - width / 2 + offsetTop.x;
            rectTarget.y = centerPos.y - width / 2 + offsetTop.y;
            if (SDL_PointInRect(&posCursor, &rectTarget)) {
                hoveredTarget = HoveredTarget::Top;
                return;
            }

            rectTarget.x = centerPos.x - width / 2 + offsetLeft.x;
            rectTarget.y = centerPos.y - width / 2 + offsetLeft.y;
            if (SDL_PointInRect(&posCursor, &rectTarget)) {
                hoveredTarget = HoveredTarget::Left;
                return;
            }

            rectTarget.x = centerPos.x - width / 2 + offsetRight.x;
            rectTarget.y = centerPos.y - width / 2 + offsetRight.y;
            if (SDL_PointInRect(&posCursor, &rectTarget)) {
                hoveredTarget = HoveredTarget::Right;
                return;
            }

            hoveredTarget = HoveredTarget::None;
        }
            break;
        case SDL_MOUSEBUTTONUP: {
            switch (hoveredTarget) {
                case Panel::HoveredTarget::Top:
                    OnClickTopArea();
                    break;
                case Panel::HoveredTarget::Left:
                    OnClickLeftArea();
                    break;
                case Panel::HoveredTarget::Right:
                    OnClickRightArea();
                    break;
                case HoveredTarget::None:
                    break;
            }

            visible = false;
        }
            break;
        default:
            break;
    }
}

void Panel::OnUpdate(SDL_Renderer *renderer)
{
    static TTF_Font *font = ResourcesManager::Instance().fontPool.find(ResID::Font_Main)->second;

    if (hoveredTarget == HoveredTarget::None)
        return;

    int val = 0;
    switch (hoveredTarget) {
        case Panel::HoveredTarget::Top:
            val = valTop;
            break;
        case Panel::HoveredTarget::Left:
            val = valLeft;
            break;
        case Panel::HoveredTarget::Right:
            val = valRight;
            break;
        case HoveredTarget::None:
            break;
    }

    SDL_DestroyTexture(texTextBackground);
    texTextBackground = nullptr;
    SDL_DestroyTexture(texTextForeground);
    texTextForeground = nullptr;

    std::string str_val = val < 0 ? "MAX" : std::to_string(val);
    SDL_Surface *sufTextBackground = TTF_RenderText_Blended(font, str_val.c_str(), colorTextBackground);
    SDL_Surface *sufTextForeground = TTF_RenderText_Blended(font, str_val.c_str(), colorTextForeground);

    widthText = sufTextBackground->w, heightText = sufTextBackground->h;
    texTextBackground = SDL_CreateTextureFromSurface(renderer, sufTextBackground);
    texTextForeground = SDL_CreateTextureFromSurface(renderer, sufTextForeground);

    SDL_FreeSurface(sufTextBackground);
    SDL_FreeSurface(sufTextForeground);
}

void Panel::OnRender(SDL_Renderer *renderer)
{
    if (!visible) return;

    SDL_Rect rectDstCursor = {centerPos.x - SIZE_TILE / 2, centerPos.y - SIZE_TILE / 2, SIZE_TILE, SIZE_TILE};
    SDL_RenderCopy(renderer, texSelectCursor, nullptr, &rectDstCursor);

    SDL_Rect rectDstPanel ={centerPos.x - width / 2, centerPos.y - height / 2, width, height};

    SDL_Texture *texPanel = nullptr;
    switch (hoveredTarget) {
        case Panel::HoveredTarget::None:
            texPanel = texIdle;
            break;
        case Panel::HoveredTarget::Top:
            texPanel = texHoveredTop;
            break;
        case Panel::HoveredTarget::Left:
            texPanel = texHoveredLeft;
            break;
        case Panel::HoveredTarget::Right:
            texPanel = texHoveredRight;
            break;
    }

    SDL_RenderCopy(renderer, texPanel, nullptr, &rectDstPanel);

    if (hoveredTarget == HoveredTarget::None)
        return;

    SDL_Rect rectDstText;

    rectDstText.x = centerPos.x - widthText / 2 + offsetShadow.x;
    rectDstText.y = centerPos.y + height / 2 + offsetShadow.y;
    rectDstText.w = widthText, rectDstText.h = heightText;
    SDL_RenderCopy(renderer, texTextBackground, nullptr, &rectDstText);

    rectDstText.x -= offsetShadow.x;
    rectDstText.y -= offsetShadow.y;
    SDL_RenderCopy(renderer, texTextForeground, nullptr, &rectDstText);
}